@echo off
title ColorBridge Launcher - AI8051U Serial Assistant
color 0E

echo                    ColorBridge Launcher
echo                  AI8051U USB-CDC Serial Assistant
echo                      Version: 2.1.7
echo                      Author: 076lik
echo ================================================================
echo.

:: Check Python environment
echo [INFO] Checking Python environment...
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found, please install Python 3.8 or higher
    echo [INFO] Download: https://www.python.org/downloads/
    pause
    exit /b 1
)

:: Display Python version
for /f "tokens=2" %%i in ('python --version 2^>^&1') do set PYTHON_VERSION=%%i
echo [SUCCESS] Python version: %PYTHON_VERSION%

:: Switch to ColorBridge directory
cd /d "%~dp0"
if errorlevel 1 (
    echo [ERROR] Cannot find ColorBridge directory
    pause
    exit /b 1
)
echo [SUCCESS] Switched to ColorBridge directory: %CD%

:: Check if main.py exists
if not exist "main.py" (
    echo [ERROR] main.py file not found
    pause
    exit /b 1
)

:: Check and install dependencies
echo.
echo [INFO] Checking Python dependencies...
if exist "requirements.txt" (
    echo [INFO] Found requirements.txt, checking dependencies...
    python -c "import PyQt6, serial, dateutil" >nul 2>&1
    if errorlevel 1 (
        echo [WARNING] Missing dependencies, installing...
        pip install -r requirements.txt
        if errorlevel 1 (
            echo [ERROR] Dependency installation failed
            pause
            exit /b 1
        )
        echo [SUCCESS] Dependencies installed
    ) else (
        echo [SUCCESS] All dependencies installed
    )
) else (
    echo [WARNING] requirements.txt not found
)

:: Display startup options
echo.
echo ================================================================
echo                        Startup Options
echo ================================================================
echo  1. Normal mode startup
echo  2. Debug mode startup (show detailed logs)
echo  3. Quiet mode startup (errors only)
echo  4. Show version info
echo  5. Show help info
echo  6. Quick start (skip menu)
echo  7. Exit program
echo ================================================================
echo.

:: Get user choice
set /p choice="Please select startup mode (1-7): "

:: Execute based on choice
if "%choice%"=="1" (
    echo.
    echo [INFO] Starting ColorBridge in normal mode...
    python main.py
) else if "%choice%"=="2" (
    echo.
    echo [INFO] Starting ColorBridge in debug mode...
    python main.py --debug
) else if "%choice%"=="3" (
    echo.
    echo [INFO] Starting ColorBridge in quiet mode...
    python main.py --quiet
) else if "%choice%"=="4" (
    echo.
    python main.py --version
    echo.
    pause
) else if "%choice%"=="5" (
    echo.
    python main.py --help
    echo.
    pause
) else if "%choice%"=="6" (
    echo.
    echo [INFO] Quick starting ColorBridge...
    start python main.py
) else if "%choice%"=="7" (
    echo.
    echo [INFO] Program exited
    pause
    exit /b 0
) else (
    echo.
    echo [ERROR] Invalid choice, please enter 1-7
    pause
    exit /b 1
)

:: Check program exit status
if errorlevel 1 (
    echo.
    echo [WARNING] ColorBridge exited abnormally, error code: %errorlevel%
    echo [INFO] Possible reasons:
    echo [INFO] - Missing dependencies
    echo [INFO] - Serial port occupied by other programs
    echo [INFO] - Python environment issues
    echo [INFO] Please check error messages and try to resolve
) else (
    echo.
    echo [SUCCESS] ColorBridge exited normally
)

echo.
echo [INFO] Press any key to exit...
pause >nul